package com.zhaopin.gitlab;

import com.zhaopin.utils.FileUtil;
import com.zhaopin.utils.GitUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.revwalk.RevCommit;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

import static com.zhaopin.utils.GitUtils.*;

@Slf4j
public class RollBackGit {
    public static void main(String[] args) throws IOException {

        String outputDir = "dir/gitlab/";
        String gitFile = "/Users/zhaopin/IdeaProjects/project/%s";

        List<String> projectNameList = FileUtils.readLines(new File(outputDir + "project_name.txt"), Charset.defaultCharset());
        for (String s : projectNameList) {

            File repoDir = new File(String.format(gitFile, s));
            //获取到上一个版本
            RevCommit nextVersion = getNextVersionLog(repoDir);


            if (null != nextVersion) {
                if (rollBackOneRevision(repoDir, nextVersion.getName())) {
                    log.info("本地回退成功");
                    if (forceUpdateHeadToRemote(repoDir)) {
                        log.info("nextVersion:" + s + ":" + nextVersion);
                    } else {
                    }
                } else {
                }
            }
        }


    }
}
